package orest.front.membercenter;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import orest.back.membermanage.RegisterUserHandler;

import net.ssta.common.DataBaseConfig;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.base.ServletBase;
import net.ssta.core.db.Record;
import net.ssta.core.web.RequestKit;

import com.yztc.rc.api.framework.entity.User;
import com.yztc.rc.api.framework.entity.UserAccount;
import com.yztc.rc.api.framework.manager.UserManager;

public class UnitUserServlet extends ServletBase {

	
	private static final long serialVersionUID = 1L;
	 

	public void defaultAction(HttpServletRequest request,HttpServletResponse response) 
	{
		register(request,response);
	}
 
	public void unitAchieveList(HttpServletRequest request,HttpServletResponse response)
		throws BaseException {
		try{
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void register(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			Record userData=kit.getRecordWithPrefix("user_");
			RegisterUserHandler handler=new RegisterUserHandler();
			
			handler.register(userData,request);
			String path=request.getContextPath();
	        response.sendRedirect(path+"/register/sendMail.jsp?Email="+userData.getString("EMAIL"));
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	
	
	
	public void verify(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			String verifyId=kit.getParameter("verifyId", "");
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.verify(verifyId);
			
			String path=request.getContextPath();
	        response.sendRedirect(path+"/register/verify.jsp");
	     }catch(BaseException se){
	    	 try{
		    	 response.setCharacterEncoding("GBK");
				 PrintWriter   out = response.getWriter();
				 out.println("该验证码不存在！");  
			     out.flush();
			     out.close();
	    	 }catch(Exception e){}
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	public void verifyUserName(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			 RequestKit kit=new RequestKit(request);
			 response.setCharacterEncoding("UTF-8");
			PrintWriter   out = response.getWriter();
				
			 String flag="false";
			String userName=kit.getParameter("userName", "");
			if("".equals(userName)){
				out.print(flag);
			    out.flush();
			    out.close();
			    return;
			}
			
			
			
			DataSource ds=DataBaseConfig.getFrameworkDataSource();
			UserManager um=UserManager.getInstance(ds);
			UserAccount au=um.getUserAccountByLoginName(userName);
			
			if(au.getUserId()!=null){
				flag="true";
			}else{
				flag="false";
			}
		    out.print(flag);
		    out.flush();
		    out.close();
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	public void resetPwd(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			String userId=kit.getParameter("userId", "");
			String password=kit.getParameter("password", "");
			
			UserManager um=UserManager.getInstance(DataBaseConfig.getFrameworkDataSource());
			UserAccount  ua=um.getUserAccountByUserId(userId);
			User user=um.getUser(userId);
			ua.setLoginPwd(password);
			um.updateUser(user, ua);
			String path=request.getContextPath();
	        response.sendRedirect(path+"/membercenter/resetPwd.jsp?flag=true");
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
 
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void modUser(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			Record userData=kit.getRecordWithPrefix("user_");
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.modUser(userData);
			String path=request.getContextPath();
			response.sendRedirect(path+"/membercenter/modUser.jsp?flag=true");
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
}
